package com.azxc.rapid.plus.system.controller;

import com.azxc.rapid.core.boot.ctrl.RapidController;
import com.azxc.rapid.core.excel.util.ExcelUtil;
import com.azxc.rapid.core.mp.support.Condition;
import com.azxc.rapid.core.mp.support.Query;
import com.azxc.rapid.core.tool.api.R;
import com.azxc.rapid.core.tool.constant.RapidConstant;
import com.azxc.rapid.core.tool.utils.DateUtil;
import com.azxc.rapid.core.tool.utils.Func;
import com.azxc.rapid.plus.system.entity.UserRegionEntity;
import com.azxc.rapid.plus.system.excel.UserRegionExcel;
import com.azxc.rapid.plus.system.excel.UserRegionImporter;
import com.azxc.rapid.plus.system.service.IUserRegionService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 控制器
 */
@RestController
@AllArgsConstructor
@RequestMapping("/systemPlus/userRegion")
@Api(value = "用户区划")
public class UserRegionController extends RapidController {

	private final IUserRegionService userRegionService;

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入userRegion")
	public R<UserRegionEntity> detail(UserRegionEntity userRegion) {
		UserRegionEntity detail = userRegionService.getOne(Condition.getQueryWrapper(userRegion));
		return R.data(detail);
	}

	/**
	 * 分页 代码自定义代号
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入userRegion")
	public R<IPage<UserRegionEntity>> list(UserRegionEntity userRegion, Query query) {
		IPage<UserRegionEntity> pages = userRegionService.page(Condition.getPage(query), Condition.getQueryWrapper(userRegion));
		return R.data(pages);
	}

	/**
	 * 新增 代码自定义代号
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入userRegion")
	public R save(@Valid @RequestBody UserRegionEntity userRegion) {
		return R.status(userRegionService.save(userRegion));
	}

	/**
	 * 修改 代码自定义代号
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入userRegion")
	public R update(@Valid @RequestBody UserRegionEntity userRegion) {
		return R.status(userRegionService.updateById(userRegion));
	}

	/**
	 * 新增或修改 代码自定义代号
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入userRegion")
	public R submit(@Valid @RequestBody UserRegionEntity userRegion) {
		return R.status(userRegionService.saveOrUpdate(userRegion));
	}


	/**
	 * 删除 代码自定义代号
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(userRegionService.removeByIds(Func.toLongList(ids)));
	}

	/**
	 * 导入
	 */
	@PostMapping("import")
	@ApiOperationSupport(order = 12)
	@ApiOperation(value = "导入", notes = "传入excel")
	public R importUserRegion(MultipartFile file, Integer isCovered) {
		UserRegionImporter userRegionImporter = new UserRegionImporter(userRegionService, isCovered == 1);
		ExcelUtil.save(file, userRegionImporter, UserRegionExcel.class);
		return R.success("操作成功");
	}

	/**
	 * 导出用户
	 */
	@GetMapping("export")
	@ApiOperationSupport(order = 13)
	@ApiOperation(value = "导出", notes = "传入")
	public void exportUserRegion(@ApiIgnore @RequestParam Map<String, Object> UserRegion, HttpServletResponse response) {
		QueryWrapper<UserRegionEntity> queryWrapper = Condition.getQueryWrapper(UserRegion, UserRegionEntity.class);

		queryWrapper.lambda().eq(UserRegionEntity::getIsDeleted, RapidConstant.DB_NOT_DELETED);
		List<UserRegionExcel> list = userRegionService.exportUserRegion(queryWrapper);
		ExcelUtil.export(response, "数据" + DateUtil.time(), "数据表", list, UserRegionExcel.class);
	}

	/**
	 * 导出模板
	 */
	@GetMapping("template")
	@ApiOperationSupport(order = 14)
	@ApiOperation(value = "导出模板")
	public void exportUser(HttpServletResponse response) {
		List<UserRegionExcel> list = new ArrayList<>();
		ExcelUtil.export(response, "数据模板", "数据表", list, UserRegionExcel.class);
	}
}
